<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="课程名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入课程名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否上架" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 是否上架" clearable>
          <el-option v-for="dict in dict.type.sys_is_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为体验课" prop="isTy">
        <el-select v-model="queryParams.isTy" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.sys_is_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <!--      <el-button-->
      <!--        type="primary"-->
      <!--        plain-->
      <!--        icon="el-icon-plus"-->
      <!--        size="mini"-->
      <!--        @click="getBase64Image"-->
      <!--      >获取海报base64图片</el-button>-->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:kc:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:kc:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:kc:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--    <div class="poster-container">-->
    <!--      <img :src="posterBase64" :width="400" :height="400" />-->
    <!--    </div>-->

    <el-table v-loading="loading" :data="kcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="课程名称" align="center" prop="title" />
      <el-table-column label="课程分类" align="center" prop="kcTypeName" />
      <el-table-column label="专区类型" align="center" prop="zqTypeName" />
      <el-table-column label="封面" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="oldPrice" />
      <el-table-column label="现价" align="center" prop="nowPrice" />
      <el-table-column label="课时" align="center" prop="ks" />
      <el-table-column label="视频" align="center" prop="video" show-overflow-tooltip />
      <el-table-column label="状态 是否上架" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="是否为体验课" align="center" prop="isTy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isTy" />
        </template>
      </el-table-column>
      <el-table-column label="是否热门" align="center" prop="isRm">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isRm" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:kc:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:kc:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="课程名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <image-upload v-model="form.image" :limit="1" />
        </el-form-item>
        <el-form-item label="详情图片" prop="img">
          <image-upload v-model="form.img" :limit="1" />
        </el-form-item>
        <el-form-item label="banner图片" prop="bimg">
          <image-upload v-model="form.bimg" :limit="1" />
        </el-form-item>
        <el-form-item label="课程分类" prop="kcType">
          <el-select v-model="form.kcType" placeholder="请选择课程分类">
            <el-option v-for="item in kcTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="专区类型" prop="zqType">
          <el-select v-model="form.zqType" placeholder="请选择专区类型">
            <el-option v-for="item in zqTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属学段" prop="teachSection">
          <el-select v-model="form.teachSection" placeholder="请选择所属学段">
            <el-option :key="0" label="小学" value="0" />
            <el-option :key="1" label="初中" value="1" />
            <el-option :key="2" label="高中" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="课程等级" prop="classLevel">
          <el-select v-model="form.classLevel" placeholder="请选择课程等级">
            <el-option :key="0" label="一级" value="0" />
            <el-option :key="1" label="二级" value="1" />
          </el-select>
          <span style="color: #909399; font-size: 12px; margin-left: 10px;">二级课程单价要比一级课程单价高</span>
        </el-form-item>
        <el-form-item label="视频" prop="video">
          <file-upload v-model="form.video" :file-type="['mp4']" :file-size="100" :limit="1" />
        </el-form-item>
        <el-form-item label="课时" prop="ks">
          <el-input-number v-model="form.ks" placeholder="请输入课时" :min="1" />
        </el-form-item>
        <el-form-item label="原价" prop="oldPrice">
          <el-input-number v-model="form.oldPrice" placeholder="请输入原价" :min="0" />
        </el-form-item>
        <el-form-item label="现价" prop="nowPrice">
          <el-input-number v-model="form.nowPrice" placeholder="请输入现价" :min="0" />
        </el-form-item>
        <el-form-item label="状态 是否上架" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_is_no" :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否为体验课" prop="isTy">
          <el-radio-group v-model="form.isTy">
            <el-radio v-for="dict in dict.type.sys_is_no" :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否热门" prop="isRm">
          <el-radio-group v-model="form.isRm">
            <el-radio v-for="dict in dict.type.sys_is_no" :key="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="课程详情">-->
        <!--          <editor v-model="form.content" :min-height="192"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKc, getKc, delKc, addKc, updateKc, findPosterImage } from '@/api/business/kc'
import { alllistKcType } from '@/api/business/kctype'
import { alllistZqType } from '@/api/business/zqtype'
export default {
  name: "Kc",
  dicts: ['sys_is_no'],
  data() {
    return {
      posterBase64: '', // 用于存储海报的 base64 编码
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      kcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        kcType: null,
        zqType: null,
        image: null,
        oldPrice: null,
        nowPrice: null,
        video: null,
        status: null,
        isTy: null,
        content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teachSection: [
          { required: true, message: "请选择所属学段", trigger: "blur" }
        ],
        classLevel: [
          { required: true, message: "请选择课程等级", trigger: "blur" }
        ],
        title: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        kcType: [
          { required: true, message: "课程分类不能为空", trigger: "blur" }
        ],
        zqType: [
          { required: true, message: "专区类型不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "封面不能为空", trigger: "blur" }
        ],
        oldPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        nowPrice: [
          { required: true, message: "现价不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态 是否上架不能为空", trigger: "blur" }
        ],
        isTy: [
          { required: true, message: "是否为体验课不能为空", trigger: "blur" }
        ],
        isRm: [
          { required: true, message: "是否热门不能为空", trigger: "blur" }
        ],
        img: [
          { required: true, message: "详情图片不能为空", trigger: "blur" }
        ],
        bimg: [
          { required: true, message: "banner图片不能为空", trigger: "blur" }
        ],
        ks: [
          { required: true, message: "课时不能为空", trigger: "blur" },
        ],

      },
      kcTypeList: [],
      zqTypeList: [],

    };
  },
  created() {
    this.getList();
    this.alllistKcType();
    this.alllistZqType();
  },
  updated() {
    // this.getList();
    // this.alllistKcType();
    // this.alllistZqType();
  },
  methods: {

    getBase64Image() {
      // 实现获取海报base64图片的逻辑
      findPosterImage({
        id: "15d82bdc-1571-4257-bea0-324e463857ff"
      }).then(response => {
        if (response.code == 200) {
          this.posterBase64 = "data:image/png;base64," + response.msg;
          console.log(this.posterBase64);
        }
      });
    },

    alllistKcType() {
      alllistKcType().then(response => {
        this.kcTypeList = response.data;
      });
    },
    alllistZqType() {
      alllistZqType().then(response => {
        this.zqTypeList = response.data;
      });
    },
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listKc(this.queryParams).then(response => {
        this.kcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        kcType: null,
        zqType: null,
        image: null,
        oldPrice: null,
        nowPrice: null,
        video: null,
        status: null,
        isTy: null,
        content: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.alllistKcType();
      this.alllistZqType();
      this.open = true;
      this.title = "添加课程信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.alllistKcType();
      this.alllistZqType();
      const id = row.id || this.ids
      getKc(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.title = this.form.title.trim();
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ks > 100000) {
            this.$modal.msgError("课时不能超过100000");
            return;
          }
          if (this.form.id != null) {
            updateKc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程信息编号为"' + ids + '"的数据项？').then(function () {
        return delKc(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/kc/export', {
        ...this.queryParams
      }, `kc_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
